package com.iims.flowable.factory;

import com.iims.common.core.domain.entity.SysUser;
import com.iims.common.core.domain.model.LoginUser;
import com.iims.common.utils.SecurityUtils;
import com.iims.common.utils.StringUtils;
import com.iims.flowable.flow.CommonUtil;
import com.iims.flowable.service.IFlowFormService;
import com.iims.flowable.service.IFlowPermissionService;
import com.iims.flowable.service.IFlowTaskFormService;
import com.iims.system.service.ISysUserService;
import org.flowable.engine.*;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;

/**
 * @author fanpei
 * @date 2021/6/24 19:31
 */
public class BaseFlowResource {

    @Autowired
    protected RepositoryService repositoryService;
    @Autowired
    protected ManagementService managementService;
    @Autowired
    protected RuntimeService runtimeService;
    @Autowired
    protected FormService formService;
    @Autowired
    protected HistoryService historyService;
    @Autowired
    protected TaskService taskService;
    @Autowired
    protected IdentityService identityService;

    @Autowired
    protected FlowResponseFactory responseFactory;

    //flowabl 权限
    @Autowired
    protected IFlowPermissionService permissionService;


    //个人应用
    @Autowired
    protected ISysUserService sysUserService;
    @Autowired
    protected IFlowTaskFormService cmisTaskFormService;
    @Autowired
    protected IFlowFormService cmisFormService;
    @Autowired
    protected FlowBusinessUtils flowBusinessUtils;


    /**
     * 获取当前登录用code
     *
     * @return
     */
    protected String getLoginUserCode() {
        return SecurityUtils.getLoginUser().getUser().getUser_code();
    }

    /**
     * 判断是否是admin
     *
     * @param userId
     * @return
     */
    public boolean isAdmin(String userId) {
        if (userId == null || userId.length() == 0) {
            LoginUser user = SecurityUtils.getLoginUser();
            return user.getUser().isAdmin();
        } else {
            SysUser sysUser = sysUserService.selectUserByCode(userId);
            return sysUser.isAdmin();
        }
    }

    /**
     * 当前用户是否为管理员
     *
     * @return
     */
    public boolean isCurrentAdmin() {
        String userId = getLoginUserCode();
        return isAdmin(userId);
    }

    protected String getUserDept(String userId) {
        if (StringUtils.isNotEmpty(userId)) {
            SysUser sysUser = sysUserService.selectUserByCode(userId);
            if (sysUser != null && null != sysUser.getDept()) {
                return sysUser.getDept().getDeptName();
            }
        }
        return "";
    }

    //region common 待修改

    protected String getUserName(String userId) {
        if (CommonUtil.isEmptyStr(userId)) {
            return "";
        }
        User user = identityService.createUserQuery().userId(userId).singleResult();
        if (user != null) {
            return user.getDisplayName();
        }
        return "";
    }

    protected String getGroupName(String groupId) {
        if (CommonUtil.isEmptyStr(groupId)) {
            return null;
        }
        Group group = identityService.createGroupQuery().groupId(groupId).singleResult();
        if (group != null) {
            return group.getName();
        }
        return null;
    }

    //endregion

    /**
     * 只接收字符串
     *
     * @param message
     * @param arguments
     * @return
     */
    protected String messageFormat(String message, String... arguments) {
        return MessageFormat.format(message, (Object[]) arguments);
    }

}
